<link rel="stylesheet" href="/static/lib/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script src="/static/lib/ztree/jquery.ztree.core.js" charset="utf-8"></script>
<script src="/static/lib/ztree/jquery.ztree.excheck.js" charset="utf-8"></script>

<SCRIPT type="text/javascript">

    var setting = {
        check: {
            enable: true,
            chkDisabledInherit: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };
    //{ id:1, pId:0, name:"随意勾选 1", open:true},
    var zNodes ={{.jsonListData}};

    function disabledNode(e) {
        var zTree = $.fn.zTree.getZTreeObj("treeGrant"),
            disabled = e.data.disabled,
            nodes = zTree.getSelectedNodes(),
            inheritParent = false, inheritChildren = false;
        if (nodes.length == 0) {
            alert("请先选择一个节点");
        }
        if (disabled) {
            inheritParent = $("#py").attr("checked");
            inheritChildren = $("#sy").attr("checked");
        } else {
            inheritParent = $("#pn").attr("checked");
            inheritChildren = $("#sn").attr("checked");
        }

        for (var i=0, l=nodes.length; i<l; i++) {
            zTree.setChkDisabled(nodes[i], disabled, inheritParent, inheritChildren);
        }
    }
    var zTree ;
    var url = '{{ .CommonIn.GetUrlByKey "admin/role/DoGrantSubmit" }}';
    var resId = {{.resId}};
    function subData (data){
        var loadIndex = layer.load(2);
        $.ajax({
            type: "post",
            url: url,
            data:data,
            success: function (res) {
                layer.close(loadIndex);
                console.log(res)
                if(res.code==0){
                    layer.alert(res.msg,function(index){
                        parent.CloseChild(true)
                    })
                }else{
                    layer.alert(res.msg)
                }
            },
            error:function(){
                layer.close(loadIndex);
            },
            complete:function (){

            }
        });
    }
    $(document).ready(function(){
        zTree = $.fn.zTree.init($("#treeGrant"), setting, zNodes);
        $("#disabledTrue").bind("click", {disabled: true}, disabledNode);
        $("#disabledFalse").bind("click", {disabled: false}, disabledNode);

    });

    layui.use([ ], function () {



        $("#cancelAdd").click(function(e){
            e.preventDefault();
            parent.CloseChild(false)
        })
        //监听提交
        $("#submitForm").click(function(e){
            e.preventDefault()
            var nodes = zTree.getCheckedNodes(true);
            var menuList = [];
            for(var i in nodes){
                menuList.push(nodes[i].id)
            }
            subData({
                resId:resId,
                menuList:menuList
            })
             
        })

    });

</SCRIPT>